//
//  TYIOSVersionSetting.m
//  Somago
//
//  Created by 谢航宇_WORK on 2022/1/6.
//  Copyright © 2022 juniu. All rights reserved.
//

#import "TYIOSVersionSetting.h"
#import <objc/runtime.h>
#import <objc/objc.h>
@implementation NSObject (TYVersionSetting)

+ (BOOL)ty_swizzleClassMethod:(SEL)originalSel with:(SEL)newSel {
    Class class = object_getClass(self);
    Method originalMethod = class_getInstanceMethod(class, originalSel);
    Method newMethod = class_getInstanceMethod(class, newSel);
    if (!originalMethod || !newMethod) return NO;
    method_exchangeImplementations(originalMethod, newMethod);
    return YES;
}

+ (BOOL)ty_swizzleInstanceMethod:(SEL)originalSel with:(SEL)newSel {
    Method originalMethod = class_getInstanceMethod(self, originalSel);
    Method newMethod = class_getInstanceMethod(self, newSel);
    if (!originalMethod || !newMethod) return NO;
    
    class_addMethod(self,
                    originalSel,
                    class_getMethodImplementation(self, originalSel),
                    method_getTypeEncoding(originalMethod));
    class_addMethod(self,
                    newSel,
                    class_getMethodImplementation(self, newSel),
                    method_getTypeEncoding(newMethod));
    
    method_exchangeImplementations(class_getInstanceMethod(self, originalSel),
                                   class_getInstanceMethod(self, newSel));
    return YES;
}

@end


@implementation UITableView (TYVersionSetting)
- (void)versionSetting {
    if (@available(iOS 15.0,*)) {
        self.sectionHeaderTopPadding = 0;
    }
    
    if (@available(iOS 11.0, *)) {
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.estimatedRowHeight = 0;
        self.estimatedSectionHeaderHeight = 0;
        self.estimatedSectionFooterHeight = 0;
    }
}

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [[self class]ty_swizzleInstanceMethod:@selector(initWithFrame:style:) with:@selector(tableViewTYInitWithFrame:style:)];
    });
}

- (instancetype)tableViewTYInitWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    [self tableViewTYInitWithFrame:frame style:style];
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    [self versionSetting];
    return self;
}
@end


@implementation UICollectionView (TYVersionSetting)
- (void)versionSetting {
    
    if (@available(iOS 11.0, *)) {
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
}

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [[self class]ty_swizzleInstanceMethod:@selector(initWithFrame:style:) with:@selector(collectionViewTYInitWithFrame:collectionViewLayout:)];
    });
}
 
- (instancetype)collectionViewTYInitWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    [self collectionViewTYInitWithFrame:frame collectionViewLayout:layout];
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    [self versionSetting];
    return self;
}
@end
